//
//  taskCellGV.swift
//  demo01
//
//  Created by babybus on 2025/7/8.
//

//
//  TaskCell.swift
//  demo01
//
//  Created by babybus on 2025/6/30.
//
import Foundation
import UIKit
import SnapKit

class TaskGVCell:UICollectionViewCell{
    private var taskImage:UIImageView = UIImageView()   //任务图片
    public var taskName:UILabel = UILabel()            //任务名称
    public var taskDetail:UILabel = UILabel()          //任务描述
    private var taskButton:MyButton = MyButton()        //任务按钮
    private var sideImage:UIImageView!   //侧边图片
    private var cellView:UIView = UIView()
    
    private var taskView = UIView()
    private var leftStackView = UIStackView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    func configure(image:UIImage,nameOfTask:String,detailOfTask:String,numOfButton:Int=0){
        //设置头像
        taskImage.image = image
        taskImage.contentMode = .scaleAspectFit
        //任务名称和描述
        taskName.text = nameOfTask
        taskDetail.text = detailOfTask
        taskName.font = UIFont.systemFont(ofSize: 15)
        taskDetail.font = UIFont.systemFont(ofSize: 13)
        taskDetail.adjustsFontSizeToFitWidth = true // 开启自动缩放
        taskDetail.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        taskDetail.textColor = .systemGray
        
        //任务按钮
        setButtom(num:numOfButton)
        
    }
    func configure(image:UIImage,nameOfTask:String,detailOfTask:String,numOfButton:Int=0,sideImg:String){
        //设置头像
        taskImage.image = image
        taskImage.contentMode = .scaleAspectFit
        //任务名称和描述
        taskName.text = nameOfTask
        taskDetail.text = detailOfTask
        taskName.font = UIFont.systemFont(ofSize: 15)
        taskDetail.font = UIFont.systemFont(ofSize: 13)
        taskDetail.adjustsFontSizeToFitWidth = true // 开启自动缩放
        taskDetail.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        taskDetail.textColor = .systemGray
        
        //任务按钮
        setButtom(num:numOfButton)
        
        //侧边图片
        self.sideImage = UIImageView()
        self.sideImage.image = UIImage(named: sideImg)
        setSideImge()
    }
    func setSideImge(){
        taskView.addSubview(self.sideImage)
        self.sideImage.snp.makeConstraints { make in
            make.width.equalTo(48)
            make.height.equalTo(18)
            make.top.equalToSuperview().inset(10)
            make.left.equalTo(taskName.snp.right).offset(4)
        }
    }
    func setButtom(num:Int){
        if num > 0{
            taskButton.setTitle("+"+String(num), for: .normal)//按钮文本
            taskButton.setTitleColor(UIColor.systemOrange.withAlphaComponent(0.9), for: .normal)//按钮文本颜色
            taskButton.layer.borderColor = UIColor.systemOrange.withAlphaComponent(0.9).cgColor//边框颜色
            taskButton.setImage(UIImage(named: "./yellowDiamond")?.withTintColor(.systemYellow,renderingMode: .alwaysOriginal),for: .normal)//图片
            taskButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)//字体设置
        }else{
            if num == 0 {
                taskButton.setTitle("明日再来", for: .normal)//按钮文本
            }else {
                taskButton.setTitle(String(num.magnitude) + "日后再来", for: .normal)//按钮文本
            }
            taskButton.setTitleColor(UIColor.systemGray, for: .normal)//按钮文本颜色
            taskButton.layer.borderColor = UIColor.systemGray5.cgColor//边框颜色
            taskButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)//字体设置
        }
        
        taskButton.backgroundColor = .white//背景色
        taskButton.layer.borderWidth = 1.0//边框
        taskButton.layer.cornerRadius = 12//圆角
        taskButton.layer.masksToBounds = true
        taskButton.titleLabel?.adjustsFontSizeToFitWidth = true//字体缩放
        taskButton.titleLabel?.minimumScaleFactor = 0.25//字体缩放最小倍数
        
        taskButton.addTarget(self, action: #selector(goNext), for: .touchUpInside)
    }
    func setupUI(){
        //任务名称和任务描述设置
        taskName.textColor = .black
        taskDetail.textColor = .black

        //将图片和（任务名称+任务描述）放到view中
        leftStackView.axis = .horizontal
        leftStackView.alignment = .fill
        leftStackView.distribution = .fill
        leftStackView.spacing = 16
        
        
        taskView.addSubview(taskName)
        taskView.addSubview(taskDetail)
        
        leftStackView.addArrangedSubview(taskImage)
        leftStackView.addArrangedSubview(taskView)
        
        cellView.addSubview(leftStackView)
        cellView.addSubview(taskButton)
        
        contentView.addSubview(cellView)
        //组件布局
        //  任务名称+任务描述+任务图片
        taskName.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(10)
            make.height.equalTo(16)
//            make.width.equalTo(199)
            make.left.equalToSuperview()
        }
        taskDetail.snp.makeConstraints { make in
            make.top.equalTo(taskName.snp.bottom).offset(6)
            make.height.equalTo(14)
//            make.width.equalTo(199)
            make.left.equalToSuperview()
        }
        taskImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.equalTo(24)
            make.height.equalTo(24)
            make.left.equalToSuperview().inset(2)
        }
        //  （任务名称+任务描述）stack + 左侧stack +按钮
        taskView.snp.makeConstraints { make in
            make.height.equalToSuperview()
            make.top.equalToSuperview()
        }
        leftStackView.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(0)
            make.height.equalToSuperview()
            make.width.equalTo(200)
        }
        taskButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
            make.width.equalTo(64)
            make.height.equalTo(26)
        }
        //最外层
        cellView.snp.makeConstraints { make in
            make.height.equalToSuperview()
            make.width.equalToSuperview()
        }
    }
    weak var delegate: CellDelegate?
    protocol CellDelegate: AnyObject {
        func didTapButtonInCell(in cell:TaskGVCell)
    }
    @objc func goNext(){
        print("按下按钮")
        delegate?.didTapButtonInCell(in: self)
        print("按钮结束")
    }
    

}

